//
//  AppDelegate.m
//  XinLang_OC
//
//  Created by WONG on 16/2/20.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "AppDelegate.h"
#import "CZMainTabBarVC.h"
#import "CZWelcomeVC.h"
#import "CZUserAccount.h"
#import "IWNewFeatureCtrl.h"
#import "CZWelcomeVC.h"
#import "CZStatusDAL.h"

#import "CZComposeVC.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    self.window.rootViewController = [self defaultVC];
    self.window.rootViewController = [CZComposeVC new];
    [self.window makeKeyAndVisible];
    

    return YES;
}

/**
 *  判断进入哪个控制器
 *  要求：如果用户没有登录，显示访客视图；如果用户登录了，再判断是否是新版本，若是进入新版本界面，否则进入欢迎界面
 */
- (UIViewController *)defaultVC {
    // 用户没有登录过，显示访客视图
    if (![CZUserAccount new].userLogin) {
        return [CZMainTabBarVC new];
    }
    return [self isNewFeature] ? [IWNewFeatureCtrl new] : [CZWelcomeVC new];
}

#define sandboxVersionKey @"sandboxVersionKey"
/**
 *  是否进入新版本
 */
- (BOOL)isNewFeature {
    // 获取当前版本
    NSString *currentVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
    // 获取上一次的版本号（偏好设置）
    NSString *localShortVersionStr = [[NSUserDefaults standardUserDefaults] objectForKey:sandboxVersionKey];
    // 保存当前版本号
    [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:sandboxVersionKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    return ![currentVersion isEqualToString:localShortVersionStr];
}


// MARK: - 其他界面切换控制器,需要通过 AppDelegate 来切换
// 目标控制器有2个: 欢迎界面和MainViewController
/**
 其他界面切换控制器
 
 - parameter isMain: true表示切换到MainViewController, false表示切换到欢迎界面
 */
- (void)switchViewController:(BOOL)isMain {
    self.window.rootViewController = isMain ? [[CZMainTabBarVC alloc] init] : [[CZWelcomeVC alloc] init];
}

// MARK: - 类方法,切换控制器,方便别人调用
/**
 类方法,切换控制器,方便别人调用
 
 - parameter isMain: true表示切换到MainViewController, false表示切换到欢迎界面
 */
+ (void)switchRootViewController:(BOOL)isMain {
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [app switchViewController:isMain];
}




- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

// 程序进入到后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    // 清除数据
    [[CZStatusDAL sharedInstance] clearCachedStatus];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
